package com.linkstec.spring;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
/**
 * 注意用jdk1.7的
 * @author 刘路
 *
 */
@Component
public class ServiceLocator implements ApplicationContextAware{
    /**
     * 用于保存接口实现类名及对应的类
     */
    private Map<String, IService> map;

    /**
     * 获取应用上下文并获取相应的接口实现类
     * @param applicationContext
     * @throws BeansException
     */
	@SuppressWarnings("unchecked")
	@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //根据接口类型返回相应的所有bean
        @SuppressWarnings({"unused" })
		Map<String, IService> map = applicationContext.getBeansOfType(IService.class);
        map2 = applicationContext.getBeansOfType(IRuleFilterHandle.class);
    }

    public Map<String, IService> getMap() {
        return map;
    }
    /**
     * 用于保存接口实现类名及对应的类
     */
    private Map<String, IRuleFilterHandle> map2;
    
    
    public Map<String, IRuleFilterHandle> getMap2() {
    	return map2;
    }
    public static void main(String[] args) {
    	System.out.println("11111111111111111");
    	
    	ServiceLocator serviceLocator = new ServiceLocator();
    	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
    	serviceLocator.setApplicationContext(applicationContext);
    	@SuppressWarnings("unchecked")
		Map<String, IRuleFilterHandle> map = applicationContext.getBeansOfType(IRuleFilterHandle.class);
		System.out.println(serviceLocator.getMap2());
		map.get("motRuleFilterHandle").dealRuleFilter(111L);
	}
}